package com.sc.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.springboot.pojo.Goods;
import com.sc.springboot.service.IGoodsService;
import com.sc.springboot.util.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author romeo
 * @since 2025-09-23 04:31:04
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    //根据关键词模糊查询商品
    @RequestMapping("/getByKeyword")
    public ResponseMsg getByKeyword(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "5") Integer size, String keyword) {
        Page<Goods> pageInfo = new Page<>(page, size);
        IPage<Goods> iPage = goodsService.page(pageInfo, new QueryWrapper<Goods>().like("name", keyword));
        if (iPage==null) {
            return ResponseMsg.getInstance(ResponseMsg.FAIL, "没有更多商品了");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, iPage);
    }

}
